/**
 * 电脑玩家
 */
function AutoRole(name) {
	// 玩家名字
	this.name = name;
	// 骰子
	this.diceArr = new Array(5);
	// 表情图片
	this.faceImages;
	this.hp = 3;
	this.handValue = [ 1, 1 ];
}

AutoRole.prototype.log = function(msg) {
	console.log(this.name + ":" + msg);
};
/**
 * 随机出骰子数组的值
 */
AutoRole.prototype.rondomDice = function() {
	for ( var i = 0; i < 5; i++) {
		this.diceArr[i] = Math.floor(1 + (Math.random() * 600) / 100);
	}
}; 
//初始化
AutoRole.prototype.init = function(){
	this.rondomDice();
	this.handValue = [ 0, 0 ];
	console.log(this.name + " init:"+ this.diceArr.toString());
};
// 提交喊的值，电脑玩家会自动生成喊的值
AutoRole.prototype.hand = function() {
	
	var isOpen = false;
	this.handValue = this.autoHand();
	if(this.handValue[0]==0 && this.handValue[1]==0){
		isOpen = true;
	}else{
		this.log("hand()"+this.handValue);
		game.currentHandValue = this.handValue;
		UI.showCpWords(this.name,game.getWords(this.handValue[0],this.handValue[1]));
	}
	return isOpen;
};
/**
 * 按一定策略得出喊的值 如果得出的值为应该开返回[0,0]
 */
AutoRole.prototype.autoHand = function() {

	var numberOfDice = 5;
	var thresholdOfOpen = 0.82;
	var numberOfValue = new Array(6);
	var diceArr = this.diceArr;
	var openCommand = [ 0, 0 ];
	
	function getProbability(number) {
		var probability = [ 0.4018775720164609, 0.16075102880658437,
				0.03215020576131687, 0.0032150205761316874, 0.0001286008230452675 ];

		if (number >= 1 && number <= number) {
			var sumOfProbability = 0;
			for ( var i = number - 1; i < probability.length; i++) {
				sumOfProbability += probability[i];
			}
			return sumOfProbability;
		} else if (number < 1) {
			return 1;
		} else {
			console.log("0");
			return 0;
		}

	}

	function initNumberOfValue() {
		for ( var i = 0; i < numberOfValue.length; i++) {
			numberOfValue[i] = 0;
		}
		for ( var i = 0; i < numberOfDice; i++) {
			numberOfValue[diceArr[i] - 1] ++;
		}
	}

	function getDecision(theOtherNumber, theOtherValue) {
		var bestNumber = 0;
		var bestValue = 0;
		var bestProbability = 0;

		for ( var i = theOtherNumber; i <= numberOfDice * 2; i++) {
			for ( var j = 1; j <= 6; j++) {
				if (i == theOtherNumber && j == 1) {
					j = theOtherValue + 1;
					if (theOtherValue >= 6) {
						continue;
					}
				}

				var tempProbability = getProbability(i - numberOfValue[j-1]);
				if (tempProbability >= bestProbability) {
					bestNumber = i;
					bestValue = j;
					bestProbability = tempProbability;
				}
			}
		}
		
		console.log("电脑选择" + bestNumber + "个" + bestValue + ",此情况发生的概率为" + bestProbability);
		if (bestProbability < (1 - thresholdOfOpen)) {
			return openCommand;
		}

		return [ bestNumber, bestValue ];
	}

	var openProbability = 0;
	var theOtherNumber = parseInt(game.gameRole.handValue[0]);
	var theOtherValue = parseInt(game.gameRole.handValue[1]);

	initNumberOfValue();
	var number = 0;
	if(theOtherValue != 0){	
		var number = numberOfValue[theOtherValue - 1];
	};

	openProbability = 1 - getProbability(theOtherNumber - number);

	if (openProbability <= thresholdOfOpen) {
		this.handValue = getDecision(theOtherNumber, theOtherValue);
	} else {
		this.handValue = openCommand;
	};
	
	return this.handValue;
};
//开,显示动画与声音
AutoRole.prototype.open = function(){
	this.log("电脑开");UI.showTip("bang");
	setTimeout(function(){
		if(game.judgeDiceAndCutdonwHP()){//电脑开对了
			game.preLoser = game.gameRole;//玩家本轮输了
			View.sound.play(0);
			UI.showCpWords(game.autoRole.name,"开");
		}
		game.judgetHPAndShowResult();
	},2000);
};

// 校验喊的值是否可行，玩家会继承这个函数
AutoRole.prototype.verify = function() {

	if (this.handValue[0] <= 0 || this.handValue[0] > 10
			|| this.handValue[1] <= 0 || this.handValue[1] > 6) {
		return false;
	}

	if (this.handValue[0] < game.autoRole.handValue[0]) {
		return false;
	} else if (this.handValue[0] == game.autoRole.handValue[0] && this.handValue[1] <= game.autoRole.handValue[1]) {
		return false;
	}

	return true;
};
// 判断输了之后减少血值
AutoRole.prototype.cutdownHP = function() {
	this.hp--;
};